<!--
 Copyright (c) 2016,2017,2018,2019,2020,2021,2022 Klaus Landsdorf (http://node-red.plus/)
 All rights reserved.
 node-red-contrib-modbus - The BSD 3-Clause License

 @author <a href="mailto:klaus.landsdorf@bianco-royal.de">Klaus Landsdorf</a> (Bianco Royal)
-->
<script type="text/javascript">
  RED.nodes.registerType('modbus-flex-write', {
    category: 'modbus',
    color: '#E9967A',
    defaults: {
      name: {value: ''},
      showStatusActivities: {value: false},
      showErrors: {value: false},
      showWarnings: {value: true},
      server: {type: 'modbus-client', required: true},
      emptyMsgOnFail: {value: false},
      keepMsgProperties: {value: false},
      delayOnStart: {value: false},
      startDelayTime: {value: ''}
    },
    inputs: 1,
    outputs: 2,
    align: "right",
    icon: 'modbus.png',
    paletteLabel: 'Modbus-Flex-Write',
    label: function () {
      return this.name || 'Modbus Flex Write'
    },
    oneditprepare: function () {
      let previous = null
      let node = this

      let tabs = RED.tabs.create({
        id: "node-input-modbus-tabs",
        onchange: function (tab) {
          $("#node-input-tabs-content").children().hide()
          $("#" + tab.id).show()
        }
      })

      tabs.addTab({
        id: "modbus-settings-tab",
        label: this._("modbus-contrib.tabs-label.settings")
      })

      tabs.addTab({
        id: "modbus-options-tab",
        label: this._("modbus-contrib.tabs-label.options")
      })

      if (this.delayOnStart) {
        $('#node-delay').show()
      }
      else {
        $('#node-delay').hide()
        $('#node-input-delayOnStart').prop('checked', false)
      }

      $('#node-input-delayOnStart').change(function () {
        if ($(this).is(':checked')) {
          $('#node-delay').show()
        } else {
          $('#node-delay').hide()
        }
      })
    }
  })
</script>


<script type="text/x-red" data-template-name="modbus-flex-write">
    <div class="form-row">
        <ul style="background: #fff; min-width: 600px; margin-bottom: 20px;" id="node-input-modbus-tabs"></ul>
    </div>
    <div id="node-input-tabs-content" style="min-height: 170px;">
        <div id="modbus-settings-tab" style="display:none">
            <div class="form-row">
                <label for="node-input-name"><i class="icon-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
                <input type="text" id="node-input-name" placeholder="Name">
            </div>
            <div class="form-row">
                <label for="node-input-server"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.server"></span></label>
                <input type="text" id="node-input-server">
            </div>
            <div class="form-row" id="node-delayonstart">
                <label for="node-input-delayOnStart" style="width:160px">
                <i class="fa fa-power-off"></i> <span data-i18n="modbus-contrib.label.delayOnStart"></span></label>
                <input type="checkbox" id="node-input-delayOnStart" style="width:40px">
            </div>
            <div class="form-row" id="node-delay">
                <label for="node-input-startDelayTime"><i class="fa fa-clock-o"></i> <span data-i18n="modbus-contrib.label.startDelayTime"></span></label>
                <input type="text" id="node-input-startDelayTime" placeholder="10" style="width:80px">
            </div>
        </div>
        <div id="modbus-options-tab" style="display:none">
            <div class="form-row">
                <label style="min-width:190px" for="node-input-emptyMsgOnFail"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.emptyMsgOnFail"></span></label>
                <input type="checkbox" id="node-input-emptyMsgOnFail" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-keepMsgProperties"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.keepMsgProperties"></span></label>
                <input type="checkbox" id="node-input-keepMsgProperties" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showStatusActivities"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showActivities"></span></label>
                <input type="checkbox" id="node-input-showStatusActivities" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showErrors"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showErrors"></span></label>
                <input type="checkbox" id="node-input-showErrors" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showWarnings"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showWarnings"></span></label>
                <input type="checkbox" checked id="node-input-showWarnings" style="max-width:30px">
            </div>
        </div>
    </div>
</script>


<script type="text/x-red" data-help-name="modbus-flex-write">
    <p>
        Modbus TCP flexible input triggered write node with connection input parameters.
    </p>
    <p>Connects to a Modbus TCP or serial to write coils/registers on each incoming msg.</p>
    <p>
        <h4>Function codes currently supported include:</h4>

        <ul>
          <li>FC 5: Force Single Coil</li>
          <li>FC 6: Preset Single Register</li>
          <li>FC 15: Force Multiple Coils</li>
          <li>FC 16: Preset Multiple Registers</li>
        </ul>
    </p>
     <p>
         <strong>Input parameter for connecting Modbus</strong>
         <ul>
             <li>unitid (0..255 tcp | 0..247 serial) - overrides default Unit-ID</li>
             <li>fc (5|6|15|16)</li>
             <li>start address (0:65535)</li>
             <li>quantity (1:65535) of coils/inputs/registers to be written from the start address</li>
         </ul>
     </p>

    <p>
        For FC 5, <strong>msg.payload</strong> must be a value of 1 or 0 or true or false.
        For FC 6, <strong>msg.payload</strong> must be a single value between 0:65535.
        For FC 16, <strong>msg.payload</strong> must be an array[] of comma separated values between 0:65535 each.
    </p>

     <p>Output 1: all given data, modbus response Buffer, input message</p>
     <p>Output 2: modbus response Buffer, all given data, input message</p>

     <p>Function node code example for single write:</p>
     <code>
        msg.payload = { value: msg.payload, 'fc': 5, 'unitid': 1, 'address': 0 , 'quantity': 1 }
        return msg
     </code>

     <p>Function node code example for multiple write:</p>
     <code>
        msg.payload = { value: msg.payload, 'fc': 15, 'unitid': 1, 'address': 0 , 'quantity': 10 }
        return msg
     </code>

</script>
